package com.ungs.readXML;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.jdom2.Comment;
import org.jdom2.DocType;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import com.ungs.utils.Constantes;

public class ReadXMLFile {
	private Logger logger = Logger.getLogger(ReadXMLFile.class);

	public void createHibernateCFG(String host, String esquema, String user,
			String pass, String tipo) {

		// xml
		Document document = new Document();
		// DocType
		DocType docType = new DocType("hibernate-configuration",
				"-//Hibernate/Hibernate Configuration DTD 3.0//EN",
				"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
		// doc
		document.setDocType(docType);
		// hibernate-configuration
		Element root = new Element("hibernate-configuration");
		// Document的根结点
		document.setRootElement(root);
		// session-factory
		Element sessionfactory = new Element("session-factory");
		// session-factory
		root.addContent(sessionfactory);
		sessionfactory.addContent(new Comment("Database connection settings"));
		// driver-class
		Element driverClass = new Element("property");
		driverClass.setAttribute("name", "connection.driver_class");
		driverClass.setText("com.mysql.jdbc.Driver");
		sessionfactory.addContent(driverClass);
		// url
		Element url = new Element("property");
		url.setAttribute("name", "connection.url");
		url.setText("jdbc:mysql://" + host + "/" + esquema);
		sessionfactory.addContent(url);
		Element username = new Element("property");
		username.setAttribute("name", "connection.username");
		username.setText(user);
		sessionfactory.addContent(username);
		Element password = new Element("property");
		password.setAttribute("name", "connection.password");
		password.setText(pass);
		sessionfactory.addContent(password);
		sessionfactory.addContent(new Comment(
				"JDBC connection pool (use the built-in)"));
		// pool
		Element pool = new Element("property");
		pool.setAttribute("name", "connection.pool_size");
		pool.setText("1");
		sessionfactory.addContent(pool);
		sessionfactory.addContent(new Comment("SQL dialect"));
		Element dialect = new Element("property");
		dialect.setAttribute("name", "dialect");
		dialect.setText("org.hibernate.dialect.MySQLDialect");
		sessionfactory.addContent(dialect);
		sessionfactory
				.addContent(new Comment("Echo all executed SQL to stdout"));
		// showsql
		Element showsql = new Element("property");
		showsql.setAttribute("name", "show_sql");
		showsql.setText("true");
		sessionfactory.addContent(showsql);
		sessionfactory.addContent(new Comment(
				"Drop and re-create the database schema on startup"));

		// hbm2ddl resive por parametro TIPO si es update o create

		Element hbm2ddl = new Element("property");
		hbm2ddl.setAttribute("name", "hbm2ddl.auto");
		hbm2ddl.setText(tipo);
		sessionfactory.addContent(hbm2ddl);

		// mapping
		sessionfactory.addContent(new Comment("mapping class"));

		Element mapping1 = new Element("mapping");
		mapping1.setAttribute("resource",
				"com/ungs/entities/mapping/Calidad.hbm.xml");
		sessionfactory.addContent(mapping1);

		Element mapping2 = new Element("mapping");
		mapping2.setAttribute("resource",
				"com/ungs/entities/mapping/Cliente.hbm.xml");
		sessionfactory.addContent(mapping2);

		Element mapping3 = new Element("mapping");
		mapping3.setAttribute("resource",
				"com/ungs/entities/mapping/CondicionEntrega.hbm.xml");
		sessionfactory.addContent(mapping3);

		Element mapping4 = new Element("mapping");
		mapping4.setAttribute("resource",
				"com/ungs/entities/mapping/DetalleCompra.hbm.xml");
		sessionfactory.addContent(mapping4);

		Element mapping5 = new Element("mapping");
		mapping5.setAttribute("resource",
				"com/ungs/entities/mapping/DetalleStock.hbm.xml");
		sessionfactory.addContent(mapping5);

		Element mapping6 = new Element("mapping");
		mapping6.setAttribute("resource",
				"com/ungs/entities/mapping/Direccion.hbm.xml");
		sessionfactory.addContent(mapping6);

		Element mapping7 = new Element("mapping");
		mapping7.setAttribute("resource",
				"com/ungs/entities/mapping/FormatoPapel.hbm.xml");
		sessionfactory.addContent(mapping7);

		Element mapping8 = new Element("mapping");
		mapping8.setAttribute("resource",
				"com/ungs/entities/mapping/Incidente.hbm.xml");
		sessionfactory.addContent(mapping8);

		Element mapping9 = new Element("mapping");
		mapping9.setAttribute("resource",
				"com/ungs/entities/mapping/Material.hbm.xml");
		sessionfactory.addContent(mapping9);

		Element mapping10 = new Element("mapping");
		mapping10.setAttribute("resource",
				"com/ungs/entities/mapping/OrdenTrabajo.hbm.xml");
		sessionfactory.addContent(mapping10);

		Element mapping11 = new Element("mapping");
		mapping11.setAttribute("resource",
				"com/ungs/entities/mapping/Persona.hbm.xml");
		sessionfactory.addContent(mapping11);

		Element mapping12 = new Element("mapping");
		mapping12.setAttribute("resource",
				"com/ungs/entities/mapping/Proceso.hbm.xml");
		sessionfactory.addContent(mapping12);

		Element mapping13 = new Element("mapping");
		mapping13.setAttribute("resource",
				"com/ungs/entities/mapping/ProcesoXOrden.hbm.xml");
		sessionfactory.addContent(mapping13);

		Element mapping15 = new Element("mapping");
		mapping15.setAttribute("resource",
				"com/ungs/entities/mapping/Proveedor.hbm.xml");
		sessionfactory.addContent(mapping15);

		Element mapping16 = new Element("mapping");
		mapping16.setAttribute("resource",
				"com/ungs/entities/mapping/SolicitudCompra.hbm.xml");
		sessionfactory.addContent(mapping16);

		Element mapping17 = new Element("mapping");
		mapping17.setAttribute("resource",
				"com/ungs/entities/mapping/StockMaterial.hbm.xml");
		sessionfactory.addContent(mapping17);

		Element mapping18 = new Element("mapping");
		mapping18.setAttribute("resource",
				"com/ungs/entities/mapping/Variante.hbm.xml");
		sessionfactory.addContent(mapping18);

		// guardo el archivo xml
		XMLOutputter outputter = new XMLOutputter();
		Format format = Format.getPrettyFormat();
		outputter.setFormat(format);
		try {
			outputter.output(document, new FileOutputStream(
					Constantes.HIBERNATE_CFG));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// if (tipo.equals(Constantes.CREATE)){
		// this.createHibernateCFG(host, esquema, user, pass,
		// Constantes.UPDATE);

		// }

	}

	public Boolean firstRun() {
		String estado = "";
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			estado = p.getProperty("firstRun");

		} catch (Exception e) {

		}

		if (estado.equals("true")) {
			return true;
		}
		return false;

	}

	public Boolean modificado() {
		String estado = "";
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			estado = p.getProperty("modificado");

		} catch (Exception e) {

		}

		if (estado.equals("true")) {
			changeModificado(false);
			return true;
		}
		return false;

	}

	@SuppressWarnings("deprecation")
	public void changeModificado(boolean change) {
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			if (change) {
				p.setProperty("modificado", "true");
			} else {
				p.setProperty("modificado", "false");
			}

			FileOutputStream out = new FileOutputStream(Constantes.CONF_INI);
			p.save(out, "config.ini_Change");
		} catch (Exception e) {

		}
	}

	@SuppressWarnings("deprecation")
	public void changeFirstRun(boolean change) {
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			if (change) {
				p.setProperty("firstRun", "false");
			} else {
				p.setProperty("firstRun", "false");
			}
			FileOutputStream out = new FileOutputStream(Constantes.CONF_INI);
			p.save(out, "config.ini_Change");
		} catch (Exception e) {
			logger.error("Error al leer la propiedad firstRun");
		}
	}

	@SuppressWarnings("deprecation")
	public void setHost(String host) {
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			p.setProperty("host", host);
			FileOutputStream out = new FileOutputStream(Constantes.CONF_INI);
			p.save(out, "config.ini_Change");
		} catch (Exception e) {
			logger.error("Error al escribir la propiedad host");
		}
	}

	public String getHost() {
		String host = "";
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			host = p.getProperty("host");

		} catch (Exception e) {
			logger.error("Error al leer la propiedad host");
		}

		return host;
	}

	@SuppressWarnings("deprecation")
	public void setEsquema(String esquema) {
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			p.setProperty("esquema", esquema);
			FileOutputStream out = new FileOutputStream(Constantes.CONF_INI);
			p.save(out, "config.ini_Change");
		} catch (Exception e) {
			logger.error("Error al escribir la propiedad esquema");
		}
	}

	public String getEsquema() {
		String esquema = "";
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			esquema = p.getProperty("esquema");

		} catch (Exception e) {
			logger.error("Error al leer la propiedad esquema");
		}

		return esquema;
	}

	@SuppressWarnings("deprecation")
	public void setUser(String user) {
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			p.setProperty("user", user);
			FileOutputStream out = new FileOutputStream(Constantes.CONF_INI);
			p.save(out, "config.ini_Change");
		} catch (Exception e) {
			logger.error("Error al escribir la propiedad user");
		}
	}

	public String getUser() {
		String user = "";
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			user = p.getProperty("user");

		} catch (Exception e) {
			logger.error("Error al leer la propiedad user");
		}

		return user;
	}

	@SuppressWarnings("deprecation")
	public void setPass(String pass) {
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			p.setProperty("pass", pass);
			FileOutputStream out = new FileOutputStream(Constantes.CONF_INI);
			p.save(out, "config.ini_Change");
		} catch (Exception e) {
			logger.error("Error al escribir la propiedad pass");
		}
	}

	public String getPass() {
		String pass = "";
		try {
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			pass = p.getProperty("pass");

		} catch (Exception e) {
			logger.error("Error al leer la propiedad pass");
		}

		return pass;
	}

}
